/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.sokoban.controller;

import br.com.sokoban.elements.interfaces.MapCharacters;
import br.com.sokoban.elements.model.Free;
import br.com.sokoban.elements.model.Player;
import br.com.sokoban.stages.controller.Board;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.JInternalFrame;
import static javax.xml.stream.XMLStreamConstants.SPACE;

/**
 *
 * @author kevinstortz
 */
public class SokobanController {
    
    private int i = 0;
    private int j = 0;
    private boolean completed = false;
    private int lvlMapa;
    
    private ArrayList<MapCharacters> mapa;
    private int numArea = 0;
    private JInternalFrame frameLevel;
    private MapCharacters player = null;
    private MapCharacters characterLeft = null;
    private MapCharacters characterRight = null;
    private MapCharacters characterTop = null;
    private MapCharacters characterDown = null;
    
    
    public SokobanController(ArrayList<MapCharacters> mapa, int lvlMapa, JInternalFrame frameLevel) {
        this.mapa = mapa;
        this.lvlMapa = lvlMapa;
        this.frameLevel = frameLevel;
        addKeyListener(new TAdapter());
        contArea();
    }
    
    private void contArea(){
        for(int i = 0; i <= mapa.size(); i ++){
            if(mapa.get(i).CARACTER == '%'){
                numArea++;
            }
        }
    }
    
    class TAdapter extends KeyAdapter {

        @Override
        public void keyPressed(KeyEvent e) {

            if (completed) {
                return;
            }
            
            setPlayers();
            
            int key = e.getKeyCode();

            MoveController mc = new MoveController();

            if (key == KeyEvent.VK_LEFT) {
                if (mc.checkCollision(player, characterLeft )) {
                    return;
                }
                
                moveCharacter(player, 1);

            } else if (key == KeyEvent.VK_RIGHT) {
                if (mc.checkCollision(player, characterRight )) {
                    return;
                }
                
                moveCharacter(player, 2);

            } else if (key == KeyEvent.VK_UP) {
                if (mc.checkCollision(player, characterTop )) {
                    return;
                }

                moveCharacter(player, 3);

            } else if (key == KeyEvent.VK_DOWN) {
                if (mc.checkCollision(player, characterDown )) {
                    return;
                }

                moveCharacter(player, 4);
            }

            frameLevel.repaint();
        }
    }

    private void moveCharacter(MapCharacters actor, int side){
        for(int i = 0; i <= mapa.size(); i++){
                    
            if(mapa.get(i).getX() == actor.getX() || mapa.get(i).getY() == actor.getY()){
                
                mapa.set(i, new Free(actor.getX(), actor.getY()));
                
                switch(side){
                        case 1: mapa.set(mapa.indexOf(characterLeft), 
                                new Player(characterLeft.getX(), characterLeft.getY()) ); break;
                        case 2: mapa.set(mapa.indexOf(characterRight), 
                                new Player(characterRight.getX(), characterRight.getY()) ); break;
                        case 3: mapa.set(mapa.indexOf(characterTop), 
                                new Player(characterTop.getX(), characterTop.getY()) ); break;
                        case 4: mapa.set(mapa.indexOf(characterDown), 
                                new Player(characterDown.getX(), characterDown.getY()) ); break;
                    }
            }
        }
    }
    
    private void setPlayers(){
            player = null;
            characterLeft = null;
            characterRight = null;
            characterTop = null;
            characterDown = null;
            
            for(int i = 0; i <= mapa.size(); i++){
                if(mapa.get(i) instanceof Player){
                    player = mapa.get(i);
                }
                
                if((player != null) || (mapa.get(i).getX() == (player.getX() -1))){
                    characterLeft = mapa.get(i);
                }
                
                if((player != null) || (mapa.get(i).getX() == (player.getX() +1))){
                    characterRight = mapa.get(i);
                }
                
                if((player != null) || (mapa.get(i).getY() == (player.getY() -1))){
                    characterTop = mapa.get(i);
                }
                
                if((player != null) || (mapa.get(i).getY() == (player.getY() +1))){
                    characterDown = mapa.get(i);
                }
            }
    }
    
    public void isCompleted() {

        int compl = 0;

        for(int i = 0; i <= mapa.size(); i ++){
            if(mapa.get(i).CARACTER == '%'){
                compl++;
            }
        }
        
        if (compl == 0) {
            completed = true;
            frameLevel.repaint();
        }
    }
}

